package cn.zd.erp.auth.organization.action;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.stereotype.Controller;

import cn.zd.erp.auth.organization.service.ebi.OrganizationEbi;
import cn.zd.erp.auth.organization.vo.Torganization;
import cn.zd.erp.auth.role.vo.Trole;
import cn.zd.erp.util.base.BaseAction;
import cn.zd.erp.util.base.Grid;
import cn.zd.erp.util.base.Json;
import cn.zd.erp.util.base.Organization;
import cn.zd.erp.util.base.PageResults;
import cn.zd.erp.util.base.Tree;
/**
 * 组织的Action类
 * @author hpx
 *
 */
@Controller(value="organizationAction")
public class OrganizationAction extends BaseAction{
	@Autowired
	private OrganizationEbi organizationEbi;
	private Torganization torganization;
	private List<Tree> tree;
	private Grid grid;
	private Json json = new Json();
	private PageResults<Organization> pageResults;
	
	public Json getJson() {
		return json;
	}


	public List<Tree> getTree() {
		return tree;
	}
	
	
	public Torganization getTorganization() {
		return torganization;
	}


	public void setTorganization(Torganization torganization) {
		this.torganization = torganization;
	}


	public Grid getGrid() {
		return grid;
	}


	public String tree()
	{
		tree = organizationEbi.getTree();
		return "tree";
		
	}
	// 页面跳转
	// 编辑用户
	public String page() {
		if(torganization!=null)
		{
			torganization = organizationEbi.get(torganization.getId());
		}
		return SUCCESS;
	}
	// 返回用户全部分页数据
	public String dataGrid() {
		grid = new Grid();
/*		page = 1;
		rows = 10;
		sort ="createdatetime";
		order="asc";*/
		pageResults = organizationEbi.dataGridCount(null, page, sort, rows, order);
		grid.setRows(pageResults.getResults());
		grid.setTotal((long) pageResults.getTotalCount());
		return "datajson";
	}
	
	// 新增用户
		public String sava() {
			/**
			 * 可能增加一个主菜单会报错
			 */
			Torganization t = organizationEbi.getByName(torganization.getCode());
			if (t != null) {
				json.setSuccess(false);
				json.setMsg("部门编号存在!");
			} else {
				try {
					torganization.setId(null);
					organizationEbi.save(torganization);
					json.setSuccess(true);
					json.setMsg("添加成功！");
				}
				catch (InvalidDataAccessApiUsageException e) {
					json.setSuccess(true);
					json.setMsg("添加成功！");
				}
				catch (Exception e) {
					System.out.println(e.toString());
					json.setMsg(e.getMessage());
				}
			}
			return "resultjson";
		}
		// 修改保存
		public String edit() {
			try {
				organizationEbi.save(torganization);
				json.setSuccess(true);
				json.setMsg("修改成功！");
			} catch (Exception e) {
				json.setMsg(e.getMessage());
			}

			return "resultjson";
		}
		
		public String delete() {
			try {
				organizationEbi.delete(torganization);
				json.setSuccess(true);
				json.setMsg("删除成功！");
			} catch (Exception e) {
				System.out.println(edit().toString());
				json.setMsg(e.getMessage());
			}
			return "resultjson";
		}
}
